package com.edooteach.api.servcice.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseUnit;
import com.edoomath.entity.course.CourseUnitHomework;
import com.edoomath.entity.course.UserCourse;
import com.edoomath.entity.user.User;
import com.edooteach.api.dao.UserDao;
import com.edooteach.api.entity.SigninRequest;
import com.edooteach.api.servcice.UserCourseService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("userCourseService")
public class UserCourseServiceImpl extends DataServiceImpl<UserCourse> implements UserCourseService {
    
    @Dao
    private UserDao userDao;
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.UserBookService#listAll(java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<UserCourse> listAll(String unionid) {
        User user = userDao.loadUser(unionid);
        return genericDaoSupport.searchForList("FROM UserCourse WHERE disabled = false AND user = :user", "user", user);
    }

    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.UserBookService#load(java.lang.Long, java.lang.String)
     */
    public UserCourse load(Long bookid, String unionid) {
        
        User user = userDao.loadUser(unionid);
        String sentence = "FROM UserCourse WHERE disabled = false AND course.id = :id AND user = :user";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("id", bookid);
        parameters.put("user", user);
        
        return genericDaoSupport.searchForObject(sentence, parameters, UserCourse.class);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.UserCourseService#listHomework(java.lang.Long, java.lang.String)
     */
    @SuppressWarnings("unchecked")
    public List<CourseUnitHomework> listHomework(Long unitId, String unionid) {
        
        CourseUnit courseUnit = genericDaoSupport.load(CourseUnit.class, unitId);
        if(courseUnit == null) {
            throw BusinessException.newInstance("500", "课程单元不存在");
        }
        
        String sentence = "FROM CourseUnitHomework WHERE disabled = false AND courseUnit = :courseUnit AND unionid = :unionid";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("courseUnit", courseUnit);
        parameters.put("unionid", unionid);
        
        return genericDaoSupport.searchForList(sentence, parameters);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.UserCourseService#load(java.lang.Long, java.lang.String, java.lang.Long)
     */
    public CourseUnitHomework load(Long id) {
        return genericDaoSupport.load(CourseUnitHomework.class, id);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edooteach.api.servcice.UserCourseService#uploadHomework(java.lang.Long, java.lang.String, com.edooteach.api.entity.SigninRequest)
     */
    public void uploadHomework(Long unitId, String unionid, SigninRequest signinRequest) {
        
        CourseUnit courseUnit = genericDaoSupport.load(CourseUnit.class, unitId);
        if(courseUnit == null) {
            throw BusinessException.newInstance("500", "课程单元不存在");
        }
        
        Course course = courseUnit.getCourse();
        UserCourse userCourse = this.load(course.getId(), unionid);
        if(userCourse == null) {
            throw BusinessException.newInstance("500", "课程不存在");
        }
        
        String images = StringUtils.join(signinRequest.getImages(), ",");
        String voices = StringUtils.join(signinRequest.getVoices(), ",");
        String voiceSeconds = StringUtils.join(signinRequest.getVoiceSeconds(), ",");
        
        CourseUnitHomework courseUnitHomework = new CourseUnitHomework(courseUnit, images, signinRequest.getTimes(), voices, voiceSeconds, unionid,
                signinRequest.getOpenid(), signinRequest.getFormId());
        genericDaoSupport.save(courseUnitHomework);
    }

}
